/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Server.MyDataType;

import Server.MyDataType.MyRowData;
import java.util.Stack;
import java.util.Vector;

/**
 *
 * @author CANG
 */



public class MyDataDynamic 
{
    public Vector<String> _vtNameColumn;    
    public Vector<MyRowData> _vtRowData;    
    public MyDataDynamic()
    {
        _vtNameColumn = new Vector<String>();
        _vtRowData = new Vector<MyRowData>();
    }
    
    public void set_vtNameColumn(String[] name)
    {
        
        for(int i = 0; i<name.length; i++)
        {
            _vtNameColumn.add(name[i]);
        }
    }
    
    // Hàm kt xem 2 myDataDynamic có giống nhau hok.
    public boolean isSameMyDataDynamic(MyDataDynamic table)
    {
        for(int i = 0; i<this._vtRowData.size(); i++)
        {
            for(int j =0;j<table._vtRowData.size(); j++)
                if(this._vtRowData.get(i).isSameRow(table._vtRowData.get(j)) != true)
                    return false;
        }
        
        return true;
    }
    
    // Loại bỏ các phần tử trùng nhau
    public MyDataDynamic RemoveSameElement()
    {
        MyDataDynamic dataTemp = new MyDataDynamic();
        dataTemp._vtNameColumn = this._vtNameColumn;
        while(this._vtRowData.size() > 0)
        {
            if(dataTemp.isContains(this._vtRowData.get(0)) != true)
            {
                dataTemp._vtRowData.add(this._vtRowData.get(0));
            }
            this._vtRowData.remove(0);
        }
        
        return dataTemp;
    }
    
    public boolean isContains(MyRowData target)
    {
        for(int i = 0; i<this._vtRowData.size(); i++)
        {
            if(this._vtRowData.get(i).isSameRow(target))
                return true;
        }
        return false;
    }
    public  Object[][] getObject()
    {
        int x = _vtRowData.size();
        int y = _vtNameColumn.size();
        Object[][] resuObjectses =  new Object[x][y];
        for(int i = 0 ; i <x ;i++)
            for(int j= 0;j<y;j++)
            {
                resuObjectses[i][j] = _vtRowData.elementAt(i).index(j);
            }
        return resuObjectses;
        
    }

    public boolean UpdateMyDataDynamic(Vector<Attribute_Value> lstSet)
    {
        for(int i = 0; i<lstSet.size(); i++)
        {
            int index = FindIndexComlumn(lstSet.get(i).NameAttribute);
            for(int j=0; j<this._vtRowData.size(); j++)
            {
                this._vtRowData.get(j).UpdateMyRowData(index, lstSet.get(i).Value);
            }
        }
        return true;
    }
    
    public int FindIndexComlumn(String col)
    {
        for(int i = 0; i<_vtNameColumn.size(); i++)
        {
            if(_vtNameColumn.get(i).equals(col))
                return i;
        }
        
        return -1;
    }
    
    public Vector<String> CreateListValueOfAttribute(String AttName)
    {
        Vector<String> lstValue = new Vector<>();
        int indexAtt = 0;
        for(int i = 0; i<this._vtNameColumn.size(); i++)
        {
            String temp1 = this._vtNameColumn.get(i);
            int index = temp1.indexOf('.');
            
            if(index >= 0)
            {
                String att1 = temp1.substring(index + 1, temp1.length());
                if(att1.toUpperCase().compareTo(AttName) == 0)
                    indexAtt = i;
            }             
            else 
            if(this._vtNameColumn.get(i).toUpperCase().compareTo(AttName) == 0)
                    indexAtt = i;
        }
        
        for(int i = 0; i<this._vtRowData.size(); i++)
        {
            lstValue.add(this._vtRowData.get(i)._vtRow.get(indexAtt));
        }
        
        return lstValue;
    }
}
